﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Paros.Domain;
using Paros.BLL;
namespace Paros.Win
{
    public partial class EntryListForm : DockContent
    {
        EntryMgr entryMgr;
        public EntryListForm()
        {
            InitializeComponent();
            entryMgr = new EntryMgr();
        }

        public void LoadData()
        {
            CategoryItem item = UserPreferences.SelectedCategory;
            if (item != null)
            {
                IList<EntryItem> list = entryMgr.GetEntriesByCategory(item.CategoryID);
                DataTable table = new DataTable();
                table.Columns.Add("ID", typeof(Int32));
                table.Columns[0].Unique = true;
                table.Columns.Add("Title");
                table.Columns.Add("Modified Time", typeof(DateTime));
                foreach (EntryItem entry in list)
                {
                    table.Rows.Add(entry.ID, entry.Title, entry.ModifiedTime);
                }

                dgvEntries.DataSource = table;
                dgvEntries.Columns["ID"].Visible = false;
                dgvEntries.Columns["Title"].Width = 400;
                dgvEntries.Columns["Modified Time"].Width = 200;
                
            }
        }


        private void EntryListForm_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((mainForm)this.TopLevelControl).OnEntryEdit();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((mainForm)this.TopLevelControl).OnEntryDelete();
        }

        private void viewToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void dgvEntries_MouseUp(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo info = dgvEntries.HitTest(e.X, e.Y);
            
            if (info != null && info.Type == DataGridViewHitTestType.Cell && info.RowIndex >= 0)
            {
                dgvEntries.Rows[info.RowIndex].Selected = true;
                EntryItem item = entryMgr.GetEntryById(Int32.Parse(dgvEntries.Rows[info.RowIndex].Cells["ID"].Value.ToString()));
                UserPreferences.SelectedEntry = item;
                ((mainForm)this.TopLevelControl).OnEntryChanged(item);
            }
        
        }

        private void dgvEntries_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvEntries.SelectedRows.Count > 0)
            {
                EntryItem item = entryMgr.GetEntryById(Int32.Parse(dgvEntries.SelectedRows[0].Cells["ID"].Value.ToString()));
                UserPreferences.SelectedEntry = item;
                ((mainForm)this.TopLevelControl).OnEntryChanged(item);
            }
        }

    }
}
